/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.action;

import java.util.List;

import org.w3c.dom.Element;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.L1Item;

/**
 * Class <code>L1ItemListedAction</code> 道具列表动作.
 * 
 * @author jrwz
 * @version 2012-6-21上午02:31:13
 * @see com.lineage.server.model.item.action
 * @since JDK1.7
 */
public final class L1ItemListedAction extends L1ItemXmlAction {

    /** 动作集合. */
    private final List<L1ItemAction> actions;

    /**
     * 道具列表动作.
     * 
     * @param element
     *            - XML 文档中的一个元素
     */
    public L1ItemListedAction(final Element element) {
        super(element);
        this.actions = L1ItemXmlParser.listActions(element);
    }

    @Override
    public L1Item execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        L1Item result = null;
        for (final L1ItemAction action : this.actions) {
            if (!action.acceptsRequest(pc, useItem)) {
                continue;
            }
            final L1Item r = action.execute(pc, useItem, readC1, readC2,
                    targetId, targetX, targetY);
            if (r != null) {
                result = r;
            }
        }
        return result;
    }

    @Override
    public L1Item executeWithAmount(final String actionName,
            final L1PcInstance pc, final L1ItemInstance item, final int amount) {
        L1Item result = null;
        for (final L1ItemAction action : this.actions) {
            if (!action.acceptsRequest(pc, item)) {
                continue;
            }
            final L1Item r = action.executeWithAmount(actionName, pc, item,
                    amount);
            if (r != null) {
                result = r;
            }
        }
        return result;
    }
}
